package com.dragon.www.smarthome.util;

import android.hardware.Camera;
import android.net.Uri;
import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.net.URI;

/**
 * Created by Administrator on 2015/11/30 16:01.
 * Author: songxy
 * Email: thinkdata@163.com
 */
public class CommonUtils {

	/**
	 *
	 * @param oldTxt
	 * @param sign
	 * @param replaceTxt
	 * @return
	 */
	public static String concatStr(String oldTxt, char sign, String replaceTxt) {
		return oldTxt.replace(String.valueOf(sign), replaceTxt);
	}

	/**
	 * 字符串转字节数组
	 * @param ip
	 * @return
	 */
	public static byte[]  convertIpToBytes(String ip) {
		String[] ips = ip.split(":|\\.");
		if (ips == null || ips.length != 4)
			throw new RuntimeException("IP 格式出错"+ip);
		byte[] result = new byte[4];
		for (int i=0; i<result.length; i++) {
			result[i] = (byte) Integer.parseInt(ips[i]);
		}
		return result;
	}

	public static final String[] colorEffect = {
			Camera.Parameters.EFFECT_NONE,
			Camera.Parameters.EFFECT_MONO,
			Camera.Parameters.EFFECT_NEGATIVE,
			Camera.Parameters.EFFECT_SOLARIZE,
			Camera.Parameters.EFFECT_SEPIA,
			Camera.Parameters.EFFECT_POSTERIZE,
			Camera.Parameters.EFFECT_WHITEBOARD,
			Camera.Parameters.EFFECT_BLACKBOARD,
			Camera.Parameters.EFFECT_AQUA};
	public static final String[] antiBanding = {
			Camera.Parameters.ANTIBANDING_AUTO,
			Camera.Parameters.ANTIBANDING_50HZ,
			Camera.Parameters.ANTIBANDING_60HZ,
			Camera.Parameters.ANTIBANDING_OFF,
	};
	public static final String[] flashMode = {
			Camera.Parameters.FLASH_MODE_OFF,
			Camera.Parameters.FLASH_MODE_AUTO,
			Camera.Parameters.FLASH_MODE_ON,
			Camera.Parameters.FLASH_MODE_RED_EYE,
			Camera.Parameters.FLASH_MODE_TORCH,
	};

	public static final String[] sceneMode = {
			Camera.Parameters.SCENE_MODE_AUTO,
			Camera.Parameters.SCENE_MODE_ACTION,
			Camera.Parameters.SCENE_MODE_PORTRAIT,
			Camera.Parameters.SCENE_MODE_LANDSCAPE,
			Camera.Parameters.SCENE_MODE_NIGHT,
			Camera.Parameters.SCENE_MODE_NIGHT_PORTRAIT,
			Camera.Parameters.SCENE_MODE_THEATRE,
			Camera.Parameters.SCENE_MODE_BEACH,
			Camera.Parameters.SCENE_MODE_SNOW,
			Camera.Parameters.SCENE_MODE_SUNSET,
			Camera.Parameters.SCENE_MODE_STEADYPHOTO,
			Camera.Parameters.SCENE_MODE_FIREWORKS,
			Camera.Parameters.SCENE_MODE_SPORTS,
			Camera.Parameters.SCENE_MODE_PARTY,
			Camera.Parameters.SCENE_MODE_CANDLELIGHT,
			Camera.Parameters.SCENE_MODE_BARCODE,
	};
	public static final String[] whiteBalance = {
			Camera.Parameters.WHITE_BALANCE_AUTO,
			Camera.Parameters.WHITE_BALANCE_INCANDESCENT,
			Camera.Parameters.WHITE_BALANCE_FLUORESCENT,
			Camera.Parameters.WHITE_BALANCE_WARM_FLUORESCENT,
			Camera.Parameters.WHITE_BALANCE_DAYLIGHT,
			Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT,
			Camera.Parameters.WHITE_BALANCE_TWILIGHT,
			Camera.Parameters.WHITE_BALANCE_SHADE,
	};
	public static final String[] focusMode = {
			Camera.Parameters.FOCUS_MODE_AUTO,
			Camera.Parameters.FOCUS_MODE_INFINITY,
			Camera.Parameters.FOCUS_MODE_MACRO,
			Camera.Parameters.FOCUS_MODE_FIXED,
			Camera.Parameters.FOCUS_MODE_EDOF,
			Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO,
	};

	public static String getSDPath() {
		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED); // �ж�sd���Ƿ����?
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// ��ȡ���Ŀ�?
			return sdDir.toString();
		} else
			return null;
	}

	public static URI getOutputFileUri() {
		File tempDir = null;
		try {
			tempDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
			if (tempDir != null && !tempDir.exists()) {
				if (!tempDir.mkdirs()) {
					System.out.println("No create file privilege");
					return null;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return tempDir.toURI();
	}

	/**
	 * 获取输出临时文件
	 *
	 * @return
	 */
	public static Uri getOutputMediaFileUri() throws IOException {
		return Uri.fromFile(getOutputMediaFile());
	}

	private static File getOutputMediaFile() throws IOException {

		File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
				Environment.DIRECTORY_PICTURES), "OCRTEST");
		// Create the storage directory if it does not exist
		if (!mediaStorageDir.exists()) {
			if (!mediaStorageDir.mkdirs()) {
				return null;
			}
		}
		// Create a media file name
		File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "image.jpg");
		if (!mediaFile.exists())
			mediaFile.createNewFile();
		return mediaFile;
	}

}
